﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Media.Imaging;
using JsonTest1.Data;

namespace JsonTest1.Net
{
    //成功
    public delegate void HttpDataStreamCalback(Stream stream, ResultData data);
    //失败(超时，数据错误.....)
    public delegate void HttpFailCalback(int failType, int errorType, ResultData data);

    public delegate void HttpProgressChangedCalback(int progress,long totalProgress, HTTP_TYPE type);

    //获取图片
    public delegate void HttpFileCallback(BitmapSource biImage);

    //获取文件

     public enum HTTP_TYPE{
            HTTP_GET,
            HTTP_POST
        };


    public class HttpEngine:IRevocable
    {
        IHttpStateListener http_state;

        private WebClient _client;
        public event Action ProcessCompleted; //完成http请求
        public event HttpDataStreamCalback OnDataStreamGenerated;
        public event HttpFailCalback OnGetFailEvent;
        public event HttpProgressChangedCalback OnProgressChanged;
        public HTTP_TYPE httpType = HTTP_TYPE.HTTP_GET;

        public HttpEngine()
        {
            _client = new WebClient();
            _client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            _client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(client_OpenReadCompleted);
            _client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
            _client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        }


        //Get方法返回
        private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs ev)
        {
            do
            {
                if (ev.Error != null || ev.Cancelled)
                {
                    break;
                }


                if (OnDataStreamGenerated != null)
                {
                    OnDataStreamGenerated(ev.Result,null);
                }


            } while (false);

            if (ProcessCompleted != null)
            {
                ProcessComplated();
            }
        }

        //Post方法返回
        private void client_OpenReadCompleted(object sender, OpenWriteCompletedEventArgs ev)
        {
            do
            {
                if (ev.Error != null || ev.Cancelled)
                {
                    break;
                }

                if (OnDataStreamGenerated != null)
                {
                    OnDataStreamGenerated(ev.Result,null);
                }


            } while (false);

            if (ProcessCompleted != null)
            {
                ProcessComplated();
            }
        }


        //下载进度条
        void client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            if (OnProgressChanged != null)
            {
                OnProgressChanged(e.ProgressPercentage,e.TotalBytesToReceive,httpType);
            }
        }

        //上传进度条

        public void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            if (OnProgressChanged != null)
            {
                OnProgressChanged(e.ProgressPercentage, e.TotalBytesToReceive, httpType);
            }
        }


        private void ProcessComplated()
        {
           // throw new NotImplementedException();
        }

        public void BeginRequest(string url)
        {
            if (httpType == HTTP_TYPE.HTTP_GET)
            {
                _client.OpenReadAsync(new Uri(url));
            }
            else if (httpType == HTTP_TYPE.HTTP_POST)
            {
                _client.OpenWriteAsync(new Uri(url));
            }

        }

        //取消请求
        public void RevokeAsync()
        {
            _client.CancelAsync();
        }
        

     public static string createStringFromIds(int[] ids){
		if(ids == null)
			return "";
		
		String query ="";
		
		foreach(int id in ids){
			query = query + id + "+";
		}
		
		return query;
	}
    }
}
